/**
 * 创建总体main界面
 */
Ext.onReady(function() {
	
	function addFrameTab(id, text, url) {
		var rPanel = Ext.getCmp('rightPanel');
//		if (rPanel == undefined) {
//			rPanel = parent.Ext.getCmp('rightPanel');
//		}
		var tab = rPanel.getComponent(id);
		var tabCount = rPanel.items.getCount();
		var htmlText = '<iframe id="frame'+id+'" src="'+ url
				+'" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
		if (tabCount <= 20) {
			if (tab) {
				rPanel.setActiveTab(tab);
			} else {
				rPanel.add({
					id: id, title: text,
					iconCls: 'tabs', //margins: '0 4 4 0',
					autoHeight: true, autoScroll: true,
					autoWidth: true, closable: true,
					listeners:{
						"beforedestroy":function(obj){
							var el = document.getElementById('frame'+id);
							if(!el){
								el=parent.document.getElementById('frame'+id);
							}
		                    if(el){
		                        el.src = 'about:blank';
		                        //try{
		                            var iframe = el.contentWindow;
		                            iframe.document.write('');
		                            iframe.close();
		                        //}catch(e){};
		                        el.parentNode.removeChild(el);
		                    }
		                    this.html=null;
		                    rPanel.remove(this);
		                    if(Ext.isIE){
		                        CollectGarbage();
		                    }
						}
					},
					html : htmlText
				}).show();
			}
		} else {
			Ext.Msg.alert("打开TAB数已超过20个，请关闭其他TAB再做操作！");
		}
	}

	var leftPanel = Ext.create('Ext.panel.Panel', {
		id : 'leftPanel',
		region : 'west',
		title : '导航栏',
		width : 200,
        split:true,
		layout : 'accordion',
		items:[
		       /*{title:"子元素1",html:"这是子元素1中的内容"},
		       {title:"子元素2",html:"这是子元素2中的内容"}
		       */
       	],
		collapsible : true
	});
	
	var rightPanel = Ext.create('Ext.tab.Panel', {
		id : 'rightPanel',
		region : 'center',
		layout : 'fit',
		items : [ {
			title : '首页',
			contentEl: 'main-welcome',
			closable : false
		} ],
		plugins : Ext.create('Ext.ux.TabCloseMenu'),
		listeners : {
			remove : function(tp, c) {
				c.hide();
			}
		}
	});
	
	var north = Ext.create('Ext.panel.Panel', {
		region : 'north',
		contentEl : 'x-north',
		autoShow : true
	});
	var south = Ext.create('Ext.panel.Panel', {
		region : 'south',
		contentEl : 'x-south',
		autoShow : true
	});
	
	new Ext.Viewport({
		id : 'main',
		layout : 'border',
		items : [ north, south, leftPanel, rightPanel ]
	});
	
	//手动定义左侧菜单栏
	var leftMenu1 = Ext.create('Ext.tree.Panel', {
		title:"系统管理",
		rootVisible : false,
		border : false,
		store : Ext.create('Ext.data.TreeStore', {
			root : {
				expanded : true,
				children : [{code:"x1", id:"001", hrefTarget:'/user/index', text:'用户管理', expanded:true, leaf:true}]
			}
		}),
		listeners : {
			'itemclick' : function(view, record, item, index, e) {
				var id = record.get('id');
				var text = record.get('text');
				var leaf = record.get('leaf');
				var url = Common.ctx + record.get('hrefTarget');
				if (leaf) {
					addFrameTab(id, text, url);
				}
			}
//				scope : this
		}
	});
	
	leftPanel.add(leftMenu1);
	/* 左侧动态菜单栏
	var buildTree = function(json) {
		return Ext.create('Ext.tree.Panel', {
			rootVisible : false,
			border : false,
			store : Ext.create('Ext.data.TreeStore', {
				root : {
					expanded : true,
					children : json.children
				}
			}),
			listeners : {
				'itemclick' : function(view, record, item, index, e) {
					var id = record.get('id');
					var text = record.get('text');
					var leaf = record.get('leaf');
					var url = '..' + Common.path + record.get('hrefTarget');
					// alert(id);
					if (leaf) {
						//addTab(id, text, url);
						console.log("打开tab");
					}
				},
				scope : this
			}
		});
	};
	
	//动态左侧树
	Ext.Ajax.request({
		url : Common.path + '/menu',
		async : false,
		success : function(response) {
			var r = response.responseText;
			console.log(r);
			//r = r.replaceAll("checked", ""); 
			r = r.replace(new RegExp("checked","gm"), "");   
			var json = Ext.JSON.decode(r);
			Ext.each(json, function(el) {
				var panel = Ext.create('Ext.panel.Panel', {
					id : el.id,
					title : el.text,
					layout : 'fit'
				});
				panel.add(buildTree(el));
				leftPanel.add(panel);
			});
		},
		failure : function(request) {
			Ext.MessageBox.show({
				title : '提示',
				msg : "操作失败",
				buttons : Ext.MessageBox.OK,
				icon : Ext.MessageBox.ERROR
			});
		},
		method : 'post'
	});
	*/
	
});